unit SoundUtil2;

interface

uses
  Windows, Forms, SysUtils, Classes, Grobal2, ExtCtrls, HUtil32, AsphyreDXSounds{, MP32WAV};

procedure PlaySound(idx: integer); overload;
procedure PlaySound(wavname: string); overload;
procedure MyPlaySound(wavname: string); overload;
procedure MyPlaySound(idx: integer); overload;
procedure PlayBGM (wavname: string);
procedure PlayMp3 (wavname: string;boFlag:Boolean);
procedure SilenceSound;
procedure PlayMapMusic(boFlag:Boolean);

implementation

uses
   ClMain, MShare, SoundUtil;

procedure MyPlaySound(wavname: string);
begin
  PlaySound(wavname);
end;
procedure MyPlaySound(idx: integer);
begin
  PlaySound(idx);
end;
procedure PlaySound (wavname: string);
begin
  if FileExists (g_ParamDir+wavname) then begin
    try
      if g_Sound <> nil then
        g_Sound.EffectFile(g_ParamDir+wavname, FALSE, FALSE);
    except
      //DebugOutStr('MyPlaySound');
    end;
  end;
end;

procedure PlaySound (idx: integer);
begin
  try
    if (g_Sound <> nil) and (not g_SndMgr.Silent) then  begin
      if (idx >= 0) and (idx < g_SndMgr.FSoundList.Count) then begin
        if g_SndMgr.FSoundList[idx] <> '' then
          if FileExists (g_ParamDir+g_SndMgr.FSoundList[idx]) then
            try
              if g_Sound <> nil then
                g_Sound.EffectFile(g_ParamDir+g_SndMgr.FSoundList[idx], FALSE, FALSE);
            except
            end;
      end;
    end;
  except
    DebugOutStr('PlaySound');
  end;
end;

procedure PlayMapMusic(boFlag:Boolean);
var
  sFileName:String;
begin
  try
    if (g_nLastMapMusic < 0){ or (not boFlag) }then begin
//      PlayMp3(g_sMapMusic,boFlag);
      //PlayMp3('',False);
      //exit;
    end else begin
      sFileName:='\Music\' + IntToStr(g_nLastMapMusic) + '.mp3';
      PlayMp3(sFileName,boFlag);
    end;
  except
    DebugOutStr('PlayMapMusic');
  end;
end;

procedure PlayBGM (wavname: string);
begin
  try
    if g_SndMgr.BGMSilent then exit;
    if g_Sound <> nil then  begin
//      if pos('Music', wavname) >  0 then
//      Application.MessageBox(PWideChar(wavname), '', 0);
      if wavname <> '' then
        if FileExists (g_ParamDir+wavname) then begin
          try
             SilenceSound;
             g_Sound.EffectFile(g_ParamDir+wavname, TRUE, FALSE);
          except
          end;
        end;
    end;
  except
    DebugOutStr('PlayBGM');
  end;
end;
procedure PlayMp3(wavname: string; boFlag:Boolean);
begin
//  if not FileExists(ExtractFilePath(wavname) + '\' + ChangeFileExt( ExtractFileName(wavname), '.wav')) then begin
//    TTHread.CreateAnonymousThread(
//    procedure begin
//      ConvertMP3toWAV(ExtractFilePath(wavname) + '\' + ExtractFileName(wavname), ExtractFilePath(wavname) + '\' + ChangeFileExt( ExtractFileName(wavname), '.wav'));
//    end
//    ).Start;
//  end else begin
//    PlayBGM(ChangeFileExt( ExtractFileName(wavname), '.wav'));
//  end;
  try
    if MP3 <> nil then  begin
       if not boFlag then begin
         Mp3.Stop;
         exit;
       end;
//      if g_SndMgr.Silent then exit;
      if wavname <> '' then begin
        if FileExists (g_ParamDir+wavname) then begin
          try
             MP3.Stop;
             MP3.Play(AnsiString(g_ParamDir+wavname));
          except
          end;
        end;
      end;
    end;
   except
     DebugOutStr('PlayMp3');
   end;
end;
procedure SilenceSound;
begin
   if g_Sound <> nil then begin
      g_Sound.Clear;
   end;
   if MP3 <> nil then begin
     MP3.Stop;
   end;
end;

end.



